﻿using Hvam.SharePoint.Search.Query.Site.Models;
using Microsoft.SharePoint.Utilities;

namespace Hvam.SharePoint.Search.Query.Site.ResultSteps
{
    internal class ImageUrlResultStep : SiteSearchResultStep
    {
        public ImageUrlResultStep(SiteSearchQuery query) : base(query)
        {
        }

        public override SiteSearchResult Process(SiteSearchResult result)
        {
            if (!result.Properties.ContainsKey("imageurl"))
            {
                var item = result.ListItem;
                if (item.File == null)
                {
                    result.Properties.Add("imageurl", "/_layouts/images/STS_ListItem16.gif");
                }
                else
                {
                    result.Properties.Add("imageurl", "/_layouts/images/" + SPUtility.MapToIcon(Query.Web, item.File.Name, ""));
                }
            }

            return result;
        }
    }
}
